package org.mapdroid.search;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;

import org.mapdroid.R;
import org.mapdroid.data.adt.MdService;
import org.mapdroid.maps.gmaps.overlay.MdMarkerItemizedOverlay;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class ServiceCategoryView extends Activity implements OnItemSelectedListener, OnItemClickListener{
	public static final String CATEGORY = "CATEGORY";

	private ListView results;
	private Spinner sp;
	private List<MdService> serviceList;
	private List<MdService> responseList;
	
	public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.service_category_view);
        
        results = (ListView) findViewById(R.id.search_category_result);
        results.setOnItemClickListener(this);
        sp = (Spinner) findViewById(R.id.spinner_category);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.category , android.R.layout.simple_spinner_item); 
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);
        sp.setOnItemSelectedListener(this);
        
        serviceList = super.getIntent().getParcelableArrayListExtra(MdMarkerItemizedOverlay.LIST);
        responseList = new LinkedList<MdService>();
        
        if(serviceList==null){
        	Toast.makeText(this, R.string.no_services_notify, Toast.LENGTH_SHORT).show();
        	finish();
        }
    }
	@Override
    public void onDestroy() {
    	super.onDestroy();
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    }

    @Override
    public void onResume(){
    	super.onResume();
    }
    
    public List<MdService> search(String text){
    	List<MdService> returnList = new LinkedList<MdService>();
    	responseList = new LinkedList<MdService>();
    	try{
			ListIterator<MdService> i = serviceList.listIterator();
			while(i.hasNext()) {
				MdService tmpService = i.next();
				String tmpString = getString(MdService.CATEGORIES_RESOURCES[tmpService.getCategory()]).toUpperCase(Locale.getDefault());
				text = text.toUpperCase(Locale.getDefault());
				
				if(tmpString.contains(text)){
					returnList.add(tmpService);
					responseList.add(tmpService);
				}
			}
			
			if(returnList.size()==0)
				return null;
			
			return returnList;
    	}catch(ClassCastException E){
    		return null;
    	}
    }
    
    /*public List<String> createNameList(List<MdService> lista){
    	try{
    		List<String> lst2 = new LinkedList<String>();
    		int controllo=0;
    		ListIterator<MdService> i = lista.listIterator();
	    	while(i.hasNext()) {
				controllo=1;
				MdService f=(MdService)i.next();
					
				
				lst2.add(f.getName());
			}
	    	if(controllo==1)
	    		return lst2;
	    	else
	    		return null;
    	}catch(ClassCastException E){
    		return null;
    	}
    }*/
    
    //Metodo per formattare la List delle Category ad una List di String
    protected List<String> formatAdapter(List<MdService> al){
    	if(al==null)
    		return null;
    	List<String> formattedCategoryList = new ArrayList<String>();
		int size = al.size();
		for(int i=0; i<size; i++){
			formattedCategoryList.add(al.get(i).getName());
		}
		if(formattedCategoryList==null)
			return null;
    	return formattedCategoryList;
    }
    
    /*@Override
    public boolean onKey(View v, int keyCode, KeyEvent event){
    	Editable name = searchName.getText();
		switch(keyCode){
			case KeyEvent.KEYCODE_DPAD_CENTER:
				
			case KeyEvent.KEYCODE_ENTER:
				List<MdService> responseList = search(name.toString());
				if(responseList!=null){
	    			List<String> strings = createNameList(responseList);
					results.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
					return true;
				}
				else{
					Toast.makeText(this, R.string.no_service_results_notify, Toast.LENGTH_LONG).show();
				}
				break;
		}
    	return false;
    }
    
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		int action = arg0.getId();
		switch(action){
			case R.id.search_b:
				Editable name = searchName.getText();
				List<MdService> responseList = search(name.toString());
				if(responseList!=null){
	    			List<String> strings = createNameList(responseList);
					results.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
				}
				else{
					Toast.makeText(this, R.string.no_service_results_notify, Toast.LENGTH_LONG).show();
				}
				break;
			case R.id.cancel_b:
				setResult(RESULT_CANCELED);
				finish();
				break;
		}
		
	}*/
    
	@Override
	public void onItemSelected(AdapterView<?> adapter, View view, int index, long arg3) {
		// TODO Auto-generated method stub
		String selectedCategory = (String) ((TextView)view).getText();
		List<MdService> list = search(selectedCategory);
		List<String> emptyList = new LinkedList<String>();
		if(list!=null)
			results.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, formatAdapter(list)));
		else
			results.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, emptyList));
			
	}
	
	@Override
	public void onNothingSelected(AdapterView<?> adapter) {
		// TODO Auto-generated method stub
	}
	
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		MdService service = responseList.get(arg2);
		boolean isParking = service.getCategory()==0 ? true : false;
		
		Intent intent = new Intent();
		intent.putExtra(MdMarkerItemizedOverlay.OBJECT, service);
		intent.putExtra(MdMarkerItemizedOverlay.OVERLAY_DATA, isParking);
		setResult(Activity.RESULT_OK, intent);
		finish();
	}
}
